import 'package:flutter/cupertino.dart';
import 'package:hitutor/Util/DateUtil.dart';
import 'package:hitutor/Util/Global.dart';
import 'package:hitutor/Util/HttpGo.dart';
import 'package:hitutor/Util/MyColor.dart';
import 'package:hitutor/Util/PageUtil.dart';
import 'package:hitutor/Widget/MyBgPage.dart';
import 'package:hitutor/Widget/MyDialog.dart';
import 'package:hitutor/Widget/MyImage.dart';

import 'WriteDiary.dart';

class MyDiary extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MyDiary();
  }
}

class _MyDiary extends State<MyDiary> {
  addDiary() {
    PageUtil().open(context, WriteDiary(onRefresh: () => getDiary()));
  }

  editDiary(i) {
    PageUtil().open(context, WriteDiary(diary: dairies[i], onRefresh: () => getDiary()));
  }

  @override
  void initState() {
    WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
      getDiary();
    });
    super.initState();
  }

  List<dynamic> dairies = [];

  getDiary() {
    HttpGo(context).get("/Dairy/GetStudentDairy?studentId=${Global.studentId}", {}, (data) {
      dairies = data["dairies"];
      setState(() {});
    }, (code, error) => showToast(error));
  }

  @override
  Widget build(BuildContext context) {
    return MyBgPage(
        "",
        Container(
            margin: EdgeInsets.fromLTRB(16, 16, 16, 24),
            padding: EdgeInsets.fromLTRB(16, 16, 16, 16),
            decoration: Global.getWhiteBg_shadow(8, MyColor.DD),
            child: Stack(alignment: Alignment.bottomRight, children: [
              Column(children: [
                Row(mainAxisAlignment: MainAxisAlignment.center, children: [
                  LocalImage("diary.png", height: 24, width: 32),
                  Text("我的日記", style: TextStyle(fontSize: 15, color: MyColor.Orange, fontWeight: FontWeight.bold))
                ]),
                Container(height: 0.5, color: MyColor.Orange, margin: EdgeInsets.only(top: 6, bottom: 16)),
                Expanded(
                    child: ListView.builder(
                        padding: EdgeInsets.all(0),
                        itemCount: dairies.length,
                        itemBuilder: (BuildContext context, int index) {
                          return ItemDiary(dairies[index], onTap: () {
                            editDiary(index);
                          });
                        }))
              ]),
              LocalImage("add_btn.png", width: 36, onTap: addDiary),
            ])));
  }
}

class ItemDiary extends StatelessWidget {
  dynamic diary;
  Function()? onTap;

  ItemDiary(this.diary, {this.onTap});

  @override
  Widget build(BuildContext context) {
    String date = getStrTime(getTimeByUtc(diary["createdDate"]), DateUtil.YMD);
    bool isSubmit = diary["isSubmitted"];
    return GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: onTap,
        child: Container(
            padding: EdgeInsets.fromLTRB(0, 8, 0, 8),
            child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
              Row(children: [
                Text(date, style: TextStyle(fontSize: 12, color: MyColor.Green)),
                Text(isSubmit ? "" : " - 暫存中", style: TextStyle(fontSize: 12, color: MyColor.C1)),
              ]),
              Text("${diary["content"]}", maxLines: 2, style: TextStyle(fontSize: 12, height: 1.5, color: MyColor.C7D))
            ])));
  }
}
